
package com.way.filemanager.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.way.filemanager.R;

import java.io.File;
import java.util.Locale;

import hwdroid.dialog.AlertDialog;
import hwdroid.dialog.DialogInterface;
import hwdroid.util.DialogUtils;


public class FileDeleteDialog {
    private static final String TAG = "FileDeleteDialog";
    Activity mActivity;
    FileDeleteDialog_OnResultCb mOnResultCb;
    private AlertDialog mProgressDialog;
    private ProgressBar mProgressBar;
    private TextView mprogress_percent;
    private TextView mprogress_message;
    Handler mHandler;
    Thread mThread;
    final FileInfo[]FileList;
    boolean bIsForceStop;
    LocalHandler mLocalHandler;
    int ProgressCount ;
    String mDelete;
    @SuppressLint("DefaultLocale")
    public FileDeleteDialog(Activity m,FileInfo[] filename) {
        mActivity = m;
        FileList = filename;
        LayoutInflater inflater = LayoutInflater.from(mActivity);
        View contentView = inflater.inflate(R.layout.custom_progress_dialog_layout, null);
        contentView.setMinimumWidth(10000);
        AlertDialog.Builder  mBuilder = new AlertDialog.Builder(mActivity);
        mBuilder.setView(contentView);
        mBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                /* User clicked cancel so do some stuff */
                bIsForceStop = true;
            }
        });
        ProgressCount = 0;
        mProgressDialog = mBuilder.create();
        mProgressBar = (ProgressBar)contentView.findViewById(R.id.progress);
        mprogress_message = (TextView)contentView.findViewById(R.id.progress_message);
        mprogress_percent = (TextView)contentView.findViewById(R.id.progress_percent);

        mProgressBar.setMax(FileList.length);
        mProgressBar.setProgress(ProgressCount);
        mprogress_message.setText(FileList[ProgressCount].GetFullPath());
        mDelete = mActivity.getResources().getString(R.string.deleting);
        String mpercent =String.format("(%d/%d)", ProgressCount, FileList.length);
        mprogress_percent.setText(mDelete + mpercent);
    }

    public void show(FileDeleteDialog_OnResultCb cb) {
        Log.v(TAG, "FileDeleteDialog show");
        mOnResultCb = cb;
        mLocalHandler = new LocalHandler();
        AlertDialog.Builder builder =  new AlertDialog.Builder(mActivity);


        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    Log.v(TAG, "FileDeleteDialog onClick ok");
                    mProgressDialog.show();
                    //DialogUtils.fromBottomToTop(mProgressDialog);

                    mThread = new Thread(new Runnable()
                    {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                               Log.v(TAG, "FileDeleteDialog run");
                               long systemtime = System.currentTimeMillis();
                               File mFile = null;
                               for(FileInfo m:FileList)
                               {
                                   Log.v(TAG, "FileDeleteDialog m.GetFullPath()"+m.GetFullPath());
                                   if(bIsForceStop)
                                       break;
                                   mFile = new File(m.GetFullPath());
                                   boolean ret = mFile.delete();
                                   mFile = null;
                                   Log.v(TAG, "FileDeleteDialog ret:"+ret);
                                   ProgressCount++;
                                   if((System.currentTimeMillis() - systemtime)>100)
                                   {
                                       mLocalHandler.removeMessages(1);
                                       mLocalHandler.sendEmptyMessage(1);
                                       systemtime = System.currentTimeMillis();
                                   }
                                   Log.v(TAG, "FileDeleteDialog sendmsg");
                               }
                               Log.v(TAG, "FileDeleteDialog end for");
                            mLocalHandler.sendEmptyMessage(2);
                        }

                    });
                    mThread.start();

                }
            });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            });

        //View customTitleView = DialogUtils.buildCustomView(mActivity, R.string.DeleteFileWarnning);
        //builder.setCustomTitle(customTitleView);
        builder.setTitle(R.string.DeleteFileWarnning);
        AlertDialog dd = builder.create();
     dd.show();
        //DialogUtils.fromBottomToTop(dd);

    }

    // public mLocalHandler = new Handler();
    @SuppressLint("HandlerLeak")
    public class LocalHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            Log.v(TAG, "FileDeleteDialog msg.what:" + msg.what);
            switch (msg.what) {
                case 1:
                    if(ProgressCount >= FileList.length)
                        ProgressCount = FileList.length -1;
                    String mpercent =String.format(Locale.getDefault(),"(%d/%d)", ProgressCount, FileList.length);
                    mprogress_percent.setText(mDelete + mpercent);
                    mProgressBar.setProgress(ProgressCount);
                    mprogress_message.setText(FileList[ProgressCount].GetFullPath());
                    break;
                case 2:
                    mLocalHandler.removeMessages(1);
                    mProgressDialog.dismiss();
                    mProgressDialog = null;

                    Log.v(TAG, "FileDeleteDialog FileDeleteResultcb:");
                    if(mOnResultCb != null)
                        mOnResultCb.FileDeleteResultcb(ProgressCount+1,CommonFunc.CalculateCommonPath(FileList));
                    Log.v(TAG, "FileDeleteDialog FileDeleteResultcb end");

                    break;
                default:
                    break;
            }
        }
    }
    public interface FileDeleteDialog_OnResultCb {
        /**
         * @param Res :Result code
         * @param r :fullpath after renamed
         */
        int FileDeleteResultcb(int Res, String r);
    }

}
